//
//  NewsInfoViewController.m
//  OtakuWorld
//
//  Created by iOS Two on 14-9-24.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "NewsInfoViewController.h"
#import "UIScrollView+TwitterCover.h"
#import "UIImageView+MJWebCache.h"

@interface NewsInfoViewController ()

@end

@implementation NewsInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated {
    self.timer=[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(changeimagesmainvc) userInfo:nil repeats:YES];
    [MobClick beginLogPageView:@"NewsInfoViewController"];

}

-(void)viewWillDisappear:(BOOL)animated {
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [appDelegate.leveyTabBarController hidesTabBar:NO animated:YES];
    [self.timer invalidate];
    self.timer = nil;
    [MobClick endLogPageView:@"NewsInfoViewController"];
}
-(void)back {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"backAR"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    [self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:0.99 green:0.62 blue:0.27 alpha:1]];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -15;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, back, nil];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
    label.text = @"漫展详情";
    label.textColor = [UIColor colorWithRed:0.99 green:0.62 blue:0.27 alpha:1];
    label.font = [UIFont fontWithName:@"STXIHEI" size:20];
    [label sizeToFit];
    self.navigationItem.titleView = label;
    
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [appDelegate.leveyTabBarController hidesTabBar:YES animated:YES];
    [super.navigationController setNavigationBarHidden:NO animated:TRUE];
    [super.navigationController setToolbarHidden:YES animated:TRUE];
    [self getData];
    // Do any additional setup after loading the view.
}

-(void)getData {
    [self showIndeterminateProgress];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://nyato.com/index.php?app=exhibition&mod=Adapi&act=ex_info&eid=%@&uid=",_eid]]];
    request.delegate = self;
    request.username = @"place";
    [request startSynchronous];
}

-(void)requestFinished:(ASIHTTPRequest *)request {
    SBJsonParser *jsonP = [[SBJsonParser alloc]init];
    if ([request.username isEqualToString:@"place"]) {
        NSDictionary *dic =[[jsonP objectWithData:[request responseData]]objectForKey:@"info"];
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://nyato.com%@",[dic objectForKey:@"cover"]]];
        
        UIView *back = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 64)];
        [self.view addSubview:back];
        
        _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, kwidth, kHeight)];
        _scroll.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_scroll];
        
        UIImageView *topImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 182)];
        [topImage setImageURL:url placeholder:[UIImage imageNamed:@"error"]];
        [_scroll addSubview:topImage];
        
        self.blurView = [[DRNRealTimeBlurView alloc] initWithFrame:CGRectMake(0, 0, 320, 182)];
        [_scroll addSubview:self.blurView];

        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 145, 320, 37)];
        titleLabel.textColor = [UIColor colorWithRed:0.25 green:0.27 blue:0.28 alpha:1];
        titleLabel.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.7];
        titleLabel.text = [NSString stringWithFormat:@"    %@",[dic objectForKey:@"name"]];
        titleLabel.font = [UIFont systemFontOfSize:20];
        [_scroll addSubview:titleLabel];
        
        UILabel *type = [[UILabel alloc]initWithFrame:CGRectMake(238, 10, 100, 100)];
        type.text = [dic objectForKey:@"section_name"];
        type.font = [UIFont systemFontOfSize:12];
        [type sizeToFit];
        type.textColor = [UIColor whiteColor];
        type.backgroundColor = [UIColor colorWithRed:0.15 green:0.8 blue:0.67 alpha:1];
        type.transform = CGAffineTransformMakeRotation(-0.3f);
        [_scroll addSubview:type];

        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 155, 87)];
        image.center = CGPointMake(_scroll.center.x, 60);
        [image setImageURL:url placeholder:[UIImage imageNamed:@"error"]];
        image.layer.borderWidth = 3;
        image.layer.borderColor = [[UIColor whiteColor] CGColor];
        [_scroll addSubview:image];
        
        UIImageView *kuang = [[UIImageView alloc]initWithFrame:CGRectMake(14, 191, 292, 22)];
        kuang.image = [UIImage imageNamed:@"3_03"];
        [_scroll addSubview:kuang];
        
        UILabel *startLabel = [[UILabel alloc]initWithFrame:CGRectMake(37, 195, 120, 15)];
        startLabel.text = [dic objectForKey:@"start_time"];
        startLabel.font = [UIFont systemFontOfSize:12];
        startLabel.textColor = [UIColor colorWithRed:0.95 green:0.6 blue:0 alpha:1];
        [_scroll addSubview:startLabel];
        
        UILabel *endLabel = [[UILabel alloc]initWithFrame:CGRectMake(182, 195, 120, 15)];
        endLabel.text = [dic objectForKey:@"end_time"];
        endLabel.font = [UIFont systemFontOfSize:12];
        endLabel.textColor = [UIColor colorWithRed:0.95 green:0.6 blue:0 alpha:1];
        [_scroll addSubview:endLabel];
        
        UIImageView *loc = [[UIImageView alloc]initWithFrame:CGRectMake(17, 225, 11, 15)];
        loc.image = [UIImage imageNamed:@"12_07"];
        [_scroll addSubview:loc];
        
        UILabel *location = [[UILabel alloc]initWithFrame:CGRectMake(34, 223, 280, 20)];
        location.textColor = [UIColor colorWithRed:0.18 green:0.59 blue:0.93 alpha:1];
        NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[dic objectForKey:@"location"]];
        NSRange contentRange = {0,[content length]};
        [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
        location.attributedText = content;
        location.font = [UIFont systemFontOfSize:15];
        location.numberOfLines = 0;
        [location sizeToFit];
        [_scroll addSubview:location];
        
        UIImageView *sale = [[UIImageView alloc]initWithFrame:CGRectMake(15, location.frame.origin.y + location.frame.size.height + 10, 13.5, 11)];
        sale.image = [UIImage imageNamed:@"12_10"];
        [_scroll addSubview:sale];
        
        UILabel *presale = [[UILabel alloc]initWithFrame:CGRectMake(33, location.frame.origin.y + location.frame.size.height + 5, 100, 20)];
        if ([[dic objectForKey:@"presale_price"] isEqualToString:@"0.00"]) {
            presale.text = [NSString stringWithFormat:@"预售: %@",@"未知"];
        } else {
            presale.text = [NSString stringWithFormat:@"预售: %@元",[dic objectForKey:@"presale_price"]];
        }
        presale.font = [UIFont systemFontOfSize:15];
        presale.textColor = [UIColor redColor];
        [_scroll addSubview:presale];
        
        UILabel *scene = [[UILabel alloc]initWithFrame:CGRectMake(140, location.frame.origin.y + location.frame.size.height + 5, 100, 20)];
        if ([[dic objectForKey:@"presale_price"] isEqualToString:@"0.00"]) {
            scene.text = [NSString stringWithFormat:@"现场: %@",@"未知"];
        } else {
            scene.text = [NSString stringWithFormat:@"现场: %@元",[dic objectForKey:@"presale_price"]];
        }        scene.font = [UIFont systemFontOfSize:15];
        scene.textColor = [UIColor redColor];
        [_scroll addSubview:scene];
        
        UIImageView *induction = [[UIImageView alloc]initWithFrame:CGRectMake(0, presale.frame.origin.y + presale.frame.size.height + 10, 320, 24.5)];
        induction.image = [UIImage imageNamed:@"erciyuan_02"];
        [_scroll addSubview:induction];
        
        _context = [[UILabel alloc]initWithFrame:CGRectMake(15, induction.frame.origin.y + induction.frame.size.height + 10, 290, 87.5)];
        if ([[dic objectForKey:@"description"] isEqualToString:@""]) {
            _context.text = @"暂无介绍";
        } else {
            _context.text = [dic objectForKey:@"description"];
        }
        _context.numberOfLines = 4;
        _context.font = [UIFont fontWithName:@"STXIHEI" size:14];
        [_context sizeToFit];
        [_context addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelClick)]];
        _context.userInteractionEnabled = YES;
        [_scroll addSubview:_context];
        
        CGSize stringSize = [[dic objectForKey:@"description"] sizeWithFont:[UIFont fontWithName:@"STXIHEI" size:14] constrainedToSize:CGSizeMake(290, 10000) lineBreakMode:NSLineBreakByTruncatingMiddle];
        if (stringSize.height > 77.5) {
            _arrow = [[UIImageView alloc]initWithFrame:CGRectMake(150, _context.frame.size.height + _context.frame.origin.y + 10, 21, 24)];
            _arrow.image = [UIImage imageNamed:@"2_03"];
            [_scroll addSubview:_arrow];
            _scroll.contentSize = CGSizeMake(kwidth, kHeight + 24);
        }
    }
    [HUD hide:YES];
}
-(void)changeimagesmainvc {
    if (_tagForImage == 0) {
        [_arrow setImage:[UIImage imageNamed:@"2_06"]];
        _tagForImage++;
    }else if(self.tagForImage == 1){
        [_arrow setImage:[UIImage imageNamed:@"2_08"]];
        _tagForImage++;
    }else if(self.tagForImage == 2){
        [_arrow setImage:[UIImage imageNamed:@"2_03"]];
        _tagForImage = 0;
    }
}
-(void)requestFailed:(ASIHTTPRequest *)request {
    NSLog(@"请求失败");
}
-(void)labelClick {
    if (_context.frame.size.height <= 87.5) {
        _context.numberOfLines = 0;
        [_context sizeToFit];
        _scroll.contentSize = CGSizeMake(kwidth, _context.frame.origin.y + _context.frame.size.height + 84);
        [_arrow removeFromSuperview];
    } else {
        _context.numberOfLines = 4;
        [_context sizeToFit];
        _scroll.contentSize = CGSizeMake(kwidth, kHeight + 24);
        [_scroll addSubview:_arrow];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
